package com.redrose.rxandroiddemo.base;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;

public abstract class BaseAdapter<T extends ViewBinding> extends RecyclerView.Adapter<BaseAdapter.BaseViewHolder<T>> {
    @NonNull
    @Override
    public BaseViewHolder<T> onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
                getLayoutRes(), parent, false);
        return new BaseViewHolder<T>((T) binding);
    }

    protected abstract int getLayoutRes();

    @Override
    public abstract int getItemCount();

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder<T> holder, int position) {
        bindView(holder.bind, position);
    }

    protected abstract void bindView(T binding, int position);

    static class BaseViewHolder<T extends ViewBinding> extends RecyclerView.ViewHolder {
        T bind;

        public BaseViewHolder(@NonNull T bind) {
            super(bind.getRoot());
            this.bind = bind;
        }
    }
}
